using System;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Swaf.BizObj;
using Swaf.Gui.AspNet2;

namespace BizMall.SiteControls
{
	public partial class MainMenu : System.Web.UI.UserControl
	{
		protected override void OnPreRender(EventArgs e)
		{
			if (SyrinxPage.ViewName.EndsWith("default.aspx"))
				Session["lastSubMenu"] = ",l1";
			else if (SyrinxPage.ViewName.EndsWith("services.aspx"))
				Session["lastSubMenu"] = "servicesSubMenu,l2";
			else if (SyrinxPage.ViewName.EndsWith("resources.aspx"))
				Session["lastSubMenu"] = "resourcesSubMenu,l3";
			else if (SyrinxPage.ViewName.EndsWith("partners.aspx"))
				Session["lastSubMenu"] = ",l4";
			else if (SyrinxPage.ViewName.EndsWith("news.aspx"))
				Session["lastSubMenu"] = "newsSubMenu,l5";
			else if (SyrinxPage.ViewName.EndsWith("aboutus.aspx"))
				Session["lastSubMenu"] = "aboutUsSubMenu,l6";
			else if (SyrinxPage.ViewName.StartsWith("admin"))
				Session["lastSubMenu"] = ",l7";
			else if (SyrinxPage.ViewName.EndsWith("contactinfo.aspx"))
				Session["lastSubMenu"] = null;

			l7.Visible = ((bool)SyrinxPage.CurrentUser.get("authenticated"));
			if (!Swaf.Cmn.IsEmpty(Session["lastSubMenu"]))
			{
				string[] ids = ((string)Session["lastSubMenu"]).Split(',');
				if (!Swaf.Cmn.IsEmpty(ids[0]))
				{
					Control c = FindControl(ids[0]);
					if (c != null)
						c.Visible = true;
				}
				if (!Swaf.Cmn.IsEmpty(ids[1]))
				{
					DivLayers dl = FindControl(ids[1]) as DivLayers;
					if (dl != null)
						dl.CssClass = "SelectedMenu";
				}
			}
			resolveChildren(this);
		}

		protected void resolveChildren(Control ctl)
		{
			foreach (Control c in ctl.Controls)
				if (c is HyperLink)
					((HyperLink)c).NavigateUrl = Swaf.Application.currentApp.resMgr.resolve(((HyperLink)c).NavigateUrl);
				else
					resolveChildren(c);
		}

		protected BasePage SyrinxPage { get { return Page as BasePage; } }
		protected bool showSubMenu()
		{
			return true;
		}

        public Swaf.IApplication App { get { return Swaf.Application.currentApp; } }
        public IBizObj CurrentUser { get { return App.securityMgr.currentPrincipal.additionalInfo as IBizObj; } }

        public Swaf.BGuid.Guid ActiveCompanyId
        {
            get
            {
                Debug.Assert(CurrentUser.get("activeCompanyId") != null);
                Debug.Assert(CurrentUser.get("activeCompanyId") is Swaf.BGuid.Guid);
                return (Swaf.BGuid.Guid)CurrentUser.get("activeCompanyId");
            }
        }

        public IBizObj ActiveCompany
        {
            get
            {
                return App.ruleMgr.execute("getCachedCompanyInfo", ActiveCompanyId).CallResults as IBizObj;
            }
        }
    }
	
}